export default {
	state:{
		list:[],
	},
	getters:{
		// 获取默认地址
		defaultAddress(state){
			return state.list.filter(v=>{
				return v.isdefault
			})
		}
	},
	mutations:{
		// 覆盖收货地址
		updateAddressList(state,{refresh,list}){
			console.log(list);
			state.list = refresh ? list : [...state.list,...list]
		},
		// 创建收货地址
		createAddress(state,item){
			state.list.unshift(item)
		},
		// 删除收货地址
		delAddress(state,index){
			state.list.splice(index,1)
		},
		// 修改收货地址
		updateAddress(state,{index,item}){
			for (let key in item) {
				state.list[index][key] = item[key]
			}
		},
		// 取消默认地址
		removeDefault(state){
			state.list.forEach((v)=>{
				if(v.isdefault){
					v.isdefault = false
				}
			})
		}
	},
	actions:{
		updateAddressAction({commit},obj){
			if(obj.item.isdefault){
				commit('removeDefault');
			}
			commit('updateAddress',obj)
		},
		// 增加地址
		createAddressAction({commit},obj){
			if(obj.item.isdefault){
				commit('removeDefault');
			}
			commit('createAddress',obj)
		}
	}
}